<template>
  <div class="wrapper">
    <div class="search">
      <div
        class="search__back iconfont icon-shangyiyehoutuifanhui"
        @click="handleBackClick"
      ></div>
      <div class="search__content">
        <span class="search__content__icon iconfont icon-search"></span>
        <input class="search__content__input" placeholder="请输入商品名称" />
      </div>
    </div>
    <ShopInfo :item="item" v-show="item.imgUrl" />
    <Content :shopName="item.name"/>
    <Cart />
  </div>
</template>

<script>
import { reactive, toRefs } from "@vue/reactivity";
import { useRouter, useRoute } from "vue-router";
import ShopInfo from "../../components/ShopInfo";
import { get } from "../../utils/request";
import Content from "./Content";
import Cart from "./Cart";

// 点击回退逻辑
const useBackRouterEffect = () => {
  const router = useRouter();
  const handleBackClick = () => {
    router.back();
  };
  return handleBackClick;
};

// 获取当前商铺信息
const useShopInfoEffect = () => {
  const route = useRoute();
  const data = reactive({ item: {} });
  const getItemData = async () => {
    const result = await get(`/api/shop/${route.params.id}`);
    if (result?.errno === 0 && result?.data) {
      data.item = result.data;
    }
  };
  const { item } = toRefs(data);
  return { item, getItemData };
};

export default {
  name: "Shop",
  components: { ShopInfo, Content, Cart },
  setup() {
    const { item, getItemData } = useShopInfoEffect();
    const handleBackClick = useBackRouterEffect();
    getItemData();
    return { item, handleBackClick };
  },
};
</script>

<style lang="scss" scoped>
.wrapper {
  padding: 0 0.18rem;
  .search {
    // background-color: pink;
    display: flex;
    margin: 0.14rem 0 0.04rem 0;
    line-height: 0.32rem;
    &__back {
      width: 0.33rem;
      height: 0.36rem;
      line-height: 0.36rem;
      text-align: start;
      font-size: 0.24rem;
      color: #b6b6b6;
      margin-left: -0.07rem;
    }
    &__content {
      display: flex;
      flex: 1;
      background: #f5f5f5;
      border-radius: 0.16rem;
      &__icon {
        width: 0.44rem;
        height: 0.34rem;
        line-height: 0.34rem;
        // background-color:pink;
        text-align: center;
        color: #b7b7b7;
      }
      &__input {
        display: block;
        width: 100%;
        padding-right: 0.2rem;
        border: none;
        outline: none;
        background: none;
        height: 0.32rem;
        font-size: 0.14rem;
        color: #333;
        &::placeholder {
          color: #333;
        }
      }
    }
  }
}
</style>
